// pages/statistics/statistics.js
import {request, requestD} from "../../request/request.js"

Page({

  /**
   * 页面的初始数据
   */
  data: {
    stis:{
      daySignin: '',
      dayAccess: '',
      dayTotal: '',
      dayWarn: ''
    },
    calendarConfig: {
      // 配置内置主题
      multi: false,
      theme: 'elegant',
      defaultDay: true,
      highlightToday: true,
      onlyShowCurrentMonth: true,
      disableMode: {
        type: 'after',
        date: false
      }
    },
    doSomeThing() {
      // 调用日历方法
      this.calendar.enableArea(['2020-3-1', '2020-12-31']);
    }
  },

  currdate:{
    year:'',
    month:'',
    day:''
  },

  afterTapDay(e){
    const {year, month, day} = e.detail
    let date = {year, month, day}
    this.updateStis(date)
    this.currdate = date

  },

  //跳转至详情页面
  handleDetail(e){
    const {filter} = e.currentTarget.dataset
    wx.navigateTo({
      url: '../recordlist/recordlist?filter=' + filter+'&date='+
      this.formatDate(this.currdate),
    })
  },

  //更新每日数据
  updateStis(date){
    requestD({
      url:'record/getRecordStatis',
      data:{
        date:this.formatDate(date)
      }
    }).then(result=>{
      const resp = result.data
      console.log(resp)
      if(resp.error_code==0){
        this.setData({
          stis: resp.data
        })
      }else{
        wx.showToast({
          title: '获取数据出错',
          icon: 'none',
          duration: 800
        });
      }
    })
  },

  formatDate(date){
    return ''+this.formatNum(date.year)
      +'-'+this.formatNum(date.month)+'-'
      +this.formatNum(date.day)
  },

  formatNum(number){
		return (number > 9 ? "" : "0") +number
	},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      mask: true,
    });
    let today = new Date()
    const date = {
      year: today.getFullYear(),
      month: today.getMonth()+1,
      day: today.getDate()
    }
    wx.hideLoading();
    this.updateStis(date)
    this.currdate = date
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})